<?php

class UserController extends Admincontroller
{
    public function init()
    {
        parent::init();
        $this->_user = new AdminModel();
        $this->_role = new RoleModel();
    }

    public function listAction()
    {
        $this->_view->assign("title", '管理员管理');
        $this->_view->assign("activetitle", '管理员管理');
        $this->_view->assign("parentmenu", '用户管理');
        $this->_view->assign("action", '管理员管理');
		
        if ($this->getRequest()->isPost()) {
            $search = $this->getRequest()->getPost('search');
            $limit = $this->getRequest()->getPost('limit');
            $offset = $this->getRequest()->getPost('offset');

            $userdata = $this->_user->selectAll($offset, $limit, $search);
            $returnarray = array(
                        "total"=> $userdata['0'],
                        "rows" => isset($userdata['1'])?$userdata['1']:[]
                    );
            exit ( json_encode($returnarray) );
        }
        $this->_view->display('User/list');
    }

    public function addAction()
    {
        $this->_view->assign("title", '新增管理员');
        $this->_view->assign("activetitle", '管理员管理');
        $this->_view->assign("parentmenu", '用户管理');
        $this->_view->assign("action", '新增管理员');
        
        if ($this->getRequest()->isPost()) {
            //校验数据
            $validator = new Validata($this->getRequest()->getPost());
            $validator->field('username')->minLength(4)->maxLength(16);
            $validator->field('email')->validEmail();
            $validator->field('password')->minLength(6)->maxLength(16);
            
            if($validator->validate())
            {
                $info['username'] = $this->getRequest()->getPost('username');
                $info['email'] = $this->getRequest()->getPost('email');
                $password = $this->getRequest()->getPost('password');
                $info['password'] = sha1($password . Yaf_Registry::get('config')->application['salt']);
                $info['role'] = $this->getRequest()->getPost('role');
                
                //检查是否同名
                if($this->_user->checkusername($info['username'])){
                    $returnarray = array('status' => '0', 'data' => '管理员不允许同名' );
                    exit ( json_encode($returnarray) );
                }
                
                //插入数据
                $return = $this->_user->insert($info);
                if ($return) {
                    $returnarray = array('status' => '1', 'data' => '新增管理员成功' );
                    exit ( json_encode($returnarray) );
                } else {
                    $returnarray = array('status' => '0', 'data' => '新增管理员失败' );
                    exit ( json_encode($returnarray) );
                }
            }
            else
            {
                //校验不通过，输出校验数据的结果
                $returnarray = array('status' => '0', 'data' => $validator->getErrorAsString(true) );
                exit ( json_encode($returnarray) );
            }
        }
        $roledata = $this->_role->getroledata();
        $this->_view->assign("roledata", $roledata);
        
        $this->_view->display('User/add');
    }

    public function editAction()
    {
        $this->_view->assign("title", '编辑管理员');
        $this->_view->assign("activetitle", '管理员管理');
        $this->_view->assign("parentmenu", '用户管理');
        $this->_view->assign("action", '编辑管理员');
        
        $id = $this->getRequest()->get('id');
        //检查权限
        if ($id == 1 && Yaf_Session::getInstance()->get("admin_uid") != 1){
            $this->_view->assign("msg", '没有权限修改超级管理员信息');
            $this->_view->display('Public/error');
        }

        if ($this->getRequest()->isPost()) {
            //校验数据
            $validator = new Validata($this->getRequest()->getPost());
            $validator->field('email')->validEmail();
            
            if($validator->validate())
            {
                $info['email'] = $this->getRequest()->getPost('email');
                $info['roleid'] = $this->getRequest()->getPost('roleid');
                $info['status'] = $this->getRequest()->getPost('status');
                $info['id'] = $this->getRequest()->getPost('id');
                
                //插入数据
                $return = $this->_user->update($info);
                if ($return) {
                    $returnarray = array('status' => '1', 'data' => '编辑用户成功' );
                    exit ( json_encode($returnarray) );
                } else {
                    $returnarray = array('status' => '0', 'data' => '编辑用户失败' );
                    exit ( json_encode($returnarray) );
                }
            }
            else
            {
                //校验不通过，输出校验数据的结果
                $returnarray = array('status' => '0', 'data' => $validator->getErrorAsString(true) );
                exit ( json_encode($returnarray) );
            }
        }
        // 获取指定id的用户信息
        
        $data = $this->_user->find($id);
        if($data){
            $roledata = $this->_role->getroledata();
            
            $this->_view->assign("roledata", $roledata);
            $this->_view->assign("data", $data);
            $this->_view->display('User/edit');
        }else{
            $this->_view->assign("msg", '指定要编辑的用户不存在');
            $this->_view->display('Public/error');
        }
    }
    
    public function changepwdAction()
    {
        $this->_view->assign("title", '修改密码');
        $this->_view->assign("activetitle", '修改密码');
        $this->_view->assign("parentmenu", '用户管理');
        $this->_view->assign("action", '修改密码');
        
        if ($this->getRequest()->isPost()) {
            //校验数据
            $validator = new Validata($this->getRequest()->getPost());
            $validator->field('password')->minLength(6)->maxLength(16);
            $validator->field('new_password')->minLength(6)->maxLength(16);
            $validator->field('new_repassword')->minLength(6)->maxLength(16);
            
            if($validator->validate())
            {
                $password = $this->getRequest()->getPost('password');
                $password = sha1($password . Yaf_Registry::get('config')->application['salt']);
                $uid = Yaf_Session::getInstance()->get("admin_uid");
                $new_password = $this->getRequest()->getPost('new_password');
                
                //检查旧密码
                $userdata = $this->_user->find(Yaf_Session::getInstance()->get("admin_uid"));
                if($userdata && $userdata->password == $password){
                    
                    //开始更新密码
                    $new_password = sha1($new_password . Yaf_Registry::get('config')->application['salt']);
                    $result  = $this->_user->updatepassword($uid, $new_password);
                    if ($result){
                        $returnarray = array('status' => '1', 'data' => '更新密码成功' );
                        exit ( json_encode($returnarray) );
                    }else{
                        $returnarray = array('status' => '0', 'data' => '更新密码失败' );
                        exit ( json_encode($returnarray) );
                    }
                }else{
                    $returnarray = array('status' => '0', 'data' => '指定用户不存在或旧密码不对' );
                    exit ( json_encode($returnarray) );
                }
            }
            else
            {
                //校验不通过，输出校验数据的结果
                $returnarray = array('status' => '0', 'data' => $validator->getErrorAsString(true) );
                exit ( json_encode($returnarray) );
            }
        }
        $this->_view->display('User/changepwd');
    }
}
